﻿#include "transdlg.h"
#include "ui_transdlg.h"
#include <QMessageBox>
#include <QPalette>

TransDlg::TransDlg(int TableId, int ParentId, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TransDlg)
{
	m_RecordBuff = NULL;
	m_CurTransTabId=0;
	m_CurIoTableId = 0;
	m_ParentId=0;
	m_TransInfoId=0;
	m_CurSeltDevId=0;
	m_CurSeltIoId=0;	
	m_CurSeltTransId=0;	
	m_bDeltTransFlag = false;

	m_CurTransTabId = TableId;
	switch (m_CurTransTabId)
	{
	case TABLE_RTRANSMIT_YX:
		{
			m_CurIoTableId = TABLE_RDEVS_YX;
		}
		break;
	case TABLE_RTRANSMIT_YC:
		{
			m_CurIoTableId = TABLE_RDEVS_YC;
		}
		break;
	case TABLE_RTRANSMIT_YK:
		{
			m_CurIoTableId = TABLE_RDEVS_YK;
		}
		break;
	case TABLE_RTRANSMIT_YM:
		{
			m_CurIoTableId = TABLE_RDEVS_YM;
		}
		break;
	}

 	m_ParentId = ParentId;
	m_TransInfoId = ParentId%PARENT_OFFSET;
	m_LastSelectItem = NULL;
    ui->setupUi(this);

	QColor	   tempcolor;
	QPalette  palette = ui->DevTreeWdgt->palette();
	tempcolor = palette.color(QPalette::Highlight);
	palette.setColor(QPalette::Highlight, tempcolor);
	palette.setColor(QPalette::HighlightedText, Qt::white);
	ui->DevTreeWdgt->setPalette(palette);
	ui->DevTreeWdgt->setAutoScroll(true);
	ui->IndexInfoTabWdgt->setCornerButtonEnabled(false);
	//ui->IndexInfoTabWdgt->verticalHeader()->setResizeMode(QHeaderView::Fixed);
	palette = ui->IndexInfoTabWdgt->palette();
	tempcolor = palette.color(QPalette::Highlight);
	palette.setColor(QPalette::Highlight, tempcolor);
	palette.setColor(QPalette::HighlightedText, Qt::white);
	ui->IndexInfoTabWdgt->setPalette(palette);
	ui->IndexInfoTabWdgt->verticalHeader()->hide();
	ui->IndexInfoTabWdgt->setSelectionBehavior(QAbstractItemView::SelectRows);   
//	ui->IndexInfoTabWdgt->setSelectionMode(QAbstractItemView::SingleSelection);   
	if (!ui->IndexInfoTabWdgt->alternatingRowColors())
	{
		ui->IndexInfoTabWdgt->setAlternatingRowColors(true);
	}
	ui->IndexInfoTabWdgt->setEditTriggers(QAbstractItemView::NoEditTriggers);

	palette = ui->TransInfoTabWdgt->palette();
	tempcolor = palette.color(QPalette::Highlight);
	palette.setColor(QPalette::Highlight, tempcolor);
	palette.setColor(QPalette::HighlightedText, Qt::white);
	ui->TransInfoTabWdgt->setPalette(palette);
	ui->TransInfoTabWdgt->setCornerButtonEnabled(false);
	ui->TransInfoTabWdgt->verticalHeader()->hide();
	ui->TransInfoTabWdgt->setSelectionBehavior(QAbstractItemView::SelectRows);   
	if (!ui->TransInfoTabWdgt->alternatingRowColors())
	{
		ui->TransInfoTabWdgt->setAlternatingRowColors(true);
	}
	ui->TransInfoTabWdgt->setEditTriggers(QAbstractItemView::NoEditTriggers);

	InitTableHeadItem();
	//ui->DevTreeWdgt->headerItem()->setText(0,QString::fromLocal8Bit("设备列表"));
	ui->DevTreeWdgt->headerItem()->setHidden(true);
	
	connect(ui->DevTreeWdgt, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(OnClickTreeItem(QTreeWidgetItem *)));

	connect(ui->IndexInfoTabWdgt, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(IndexTabDoubleClick()));

	connect(ui->pBtn_InsertOne, SIGNAL(clicked()), this, SLOT(AddSeltIoInfo()));
	connect(ui->pBtn_InsertAll, SIGNAL(clicked()), this, SLOT(AddAll()));
	connect(ui->pBtn_DeltOne, SIGNAL(clicked()), this, SLOT(DeleteSeltTrans()));
	connect(ui->pBtn_DeltAll, SIGNAL(clicked()), this, SLOT(DeleteAllTrans()));
	connect(ui->pBtn_DeltCancel, SIGNAL(clicked()), this, SLOT(CancelDeleteTrans()));
	connect(ui->pBtn_Affirm, SIGNAL(clicked()), this, SLOT(Affirm()));
	connect(ui->pBtn_Save, SIGNAL(clicked()), this, SLOT(SaveBtnClick()));
	connect(ui->pBtn_Cancel, SIGNAL(clicked()), this, SLOT(close()));

	InitTreeShow();
	ShowTransInfo();
}

TransDlg::~TransDlg()
{
    delete ui;
}

void TransDlg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//初始化列表头
void TransDlg::InitTableHeadItem()
{
	ui->IndexInfoTabWdgt->setColumnCount(2);
	QTableWidgetItem *HeadItem = NULL;
	HeadItem = new QTableWidgetItem;
	HeadItem->setText(QString::fromLocal8Bit("点号"));
	HeadItem->setTextAlignment(Qt::AlignHCenter);
	ui->IndexInfoTabWdgt->setHorizontalHeaderItem(0, HeadItem);
	ui->IndexInfoTabWdgt->setColumnWidth(0, 40);

	HeadItem = new QTableWidgetItem;

	switch (m_CurIoTableId)
	{
	case TABLE_RDEVS_YX:
		{
			HeadItem->setText(QString::fromLocal8Bit("遥信名称"));
		}
		break;
	case TABLE_RDEVS_YC:
		{
			HeadItem->setText(QString::fromLocal8Bit("遥测名称"));
		}
		break;
	case TABLE_RDEVS_YK:
		{
			HeadItem->setText(QString::fromLocal8Bit("遥控名称"));
		}
		break;
	case TABLE_RDEVS_YM:
		{
			HeadItem->setText(QString::fromLocal8Bit("遥脉名称"));
		}
		break;
	default:
		break;
	}

	HeadItem->setTextAlignment(Qt::AlignHCenter);
	ui->IndexInfoTabWdgt->setHorizontalHeaderItem(1, HeadItem);
	ui->IndexInfoTabWdgt->setColumnWidth(1, 300);

	ui->TransInfoTabWdgt->setColumnCount(3);
	HeadItem = new QTableWidgetItem;
	HeadItem->setText(QString::fromLocal8Bit("序号"));
	HeadItem->setTextAlignment(Qt::AlignHCenter);
	ui->TransInfoTabWdgt->setHorizontalHeaderItem(0, HeadItem);
	ui->TransInfoTabWdgt->setColumnWidth(0, 40);
	HeadItem = new QTableWidgetItem;
	HeadItem->setText(QString::fromLocal8Bit("设备"));
	HeadItem->setTextAlignment(Qt::AlignHCenter);
	ui->TransInfoTabWdgt->setHorizontalHeaderItem(1, HeadItem);
	ui->TransInfoTabWdgt->setColumnWidth(1, 120);

	HeadItem = new QTableWidgetItem;

	switch (m_CurTransTabId)
	{
	case TABLE_RTRANSMIT_YX:
		{
			HeadItem->setText(QString::fromLocal8Bit("转发遥信名称"));
		}
		break;
	case TABLE_RTRANSMIT_YC:
		{
			HeadItem->setText(QString::fromLocal8Bit("转发遥测名称"));
		}
		break;
	case TABLE_RTRANSMIT_YK:
		{
			HeadItem->setText(QString::fromLocal8Bit("转发遥控名称"));
		}
		break;
	case TABLE_RTRANSMIT_YM:
		{
			HeadItem->setText(QString::fromLocal8Bit("转发遥脉名称"));
		}
		break;
	default:
		break;
	}

	HeadItem->setTextAlignment(Qt::AlignHCenter);
	ui->TransInfoTabWdgt->setHorizontalHeaderItem(2, HeadItem);
	ui->TransInfoTabWdgt->setColumnWidth(2, 300);

}

//初始化树显示
void TransDlg::InitTreeShow()
{
	if (m_ParentId<=0)
	{
		return ;
	}
	ui->DevTreeWdgt->clear();

	QTreeWidgetItem *pNewItem = NULL;
	
	QString			ItemText;
	TreeItemData	ItemUserData;	
	QVariant		ItemVariData;

	pNewItem = new QTreeWidgetItem(ui->DevTreeWdgt);
	ItemText = QString::fromLocal8Bit("任务列表");
	ItemVariData.setValue<TreeItemData>(ItemUserData);
	SetItem(pNewItem, ItemText, ItemVariData);
	ui->DevTreeWdgt->addTopLevelItem(pNewItem);
	pNewItem->setExpanded(true);
	if (pNewItem)
	{
		InitTreeItem_Task(pNewItem);
	}
}

//设置树Item
int TransDlg::SetItem(QTreeWidgetItem *pTreeItem, QString ItemText, QVariant ItemData)
{
	if (!pTreeItem)
		return -1;
	QSize size(50, 16);
	pTreeItem->setText(0, ItemText);
	pTreeItem->setData(0, Qt::ToolTipRole, ItemText);
	pTreeItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	pTreeItem->setData(0, Qt::UserRole, ItemData);
	pTreeItem->setIcon(0, QIcon(":/images/open.png"));
	pTreeItem->setSizeHint(0,size);
	return 1;
}

void TransDlg::InitTreeItem_Task(QTreeWidgetItem *pParentItem)
{
	if (!pParentItem)
	{
		return;
	}
	char	*buff = NULL;
	int	buff_len = 0;
	int	Ret = 0;
	QTreeWidgetItem *pNewItem=NULL;
	QString			ItemText;
	TreeItemData	ItemUserData;	
	QVariant		ItemVariData;

	pNewItem = new QTreeWidgetItem(pParentItem);
	ItemText = QString::fromLocal8Bit("终端");
	ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
	ItemUserData.ShowTableId = 0;
	ItemUserData.RecordId = 0;
	ItemVariData.setValue<TreeItemData>(ItemUserData);
	SetItem(pNewItem, ItemText, ItemVariData);
	if (pNewItem )
	{
		InitTreeItem_Dev(pNewItem, TABLE_RPARAMS_TERMINAL*PARENT_OFFSET);
	}

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId(SYSTEM_TYPE_NSA_YD, TABLE_RTASK_INFO,buff, &buff_len, true);
	if (Ret>0 && buff_len>0 && buff)
	{
		for (int i=0; i<Ret; i++)
		{
			pNewItem = new QTreeWidgetItem(pParentItem);
			ItemText = QString::fromLocal8Bit(((rTask_Info*)buff)[i].Name);
			ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
			ItemUserData.ShowTableId = TABLE_RTASK_INFO;
			ItemUserData.RecordId = ((rTask_Info*)buff)[i].ID;
			ItemVariData.setValue<TreeItemData>(ItemUserData);
			SetItem(pNewItem, ItemText, ItemVariData);
			if (pNewItem )
			{
				InitTreeItem_Dev(pNewItem, ((rTask_Info*)buff)[i].ID+TABLE_RTASK_INFO*PARENT_OFFSET);
			}
		}

	}
	if (buff)
	{
		free(buff);
		buff = NULL;
	}

}

void TransDlg::InitTreeItem_Dev(QTreeWidgetItem *pParentItem, unsigned int ParentId)
{
	if (!pParentItem || ParentId<=0)
	{
		return;
	}
	char	*buff = NULL;
	int	buff_len = 0;
	int	Ret = 0;
	QTreeWidgetItem *pNewItem=NULL;
	QString			ItemText;
	TreeItemData	ItemUserData;	
	QVariant		ItemVariData;

	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, TABLE_RDEVS_INFO, ParentId, buff, &buff_len, true);
	if (Ret>0 && buff_len>0 && buff)
	{
		for (int i=0; i<Ret; i++)
		{
			pNewItem = new QTreeWidgetItem(pParentItem);
			ItemText = QString::fromLocal8Bit(((rDevS_Info*)buff)[i].Name);
			ItemUserData.AppType = SYSTEM_TYPE_NSA_YD;
			ItemUserData.ShowTableId = TABLE_RDEVS_INFO;
			ItemUserData.RecordId = ((rDevS_Info*)buff)[i].ID;
			ItemVariData.setValue<TreeItemData>(ItemUserData);
			SetItem(pNewItem, ItemText, ItemVariData);
		}
	}
	if (buff)
	{
		free(buff);
		buff = NULL;
	}
}


void TransDlg::OnClickTreeItem(QTreeWidgetItem *pCurItem)
{
	if (!pCurItem)
	{
		return ;
	}
	if (!pCurItem->isExpanded())
	{
		ui->DevTreeWdgt->setItemExpanded(pCurItem, true);
	}
	if (pCurItem != m_LastSelectItem)
	{
		if (m_LastSelectItem)
		{
			m_LastSelectItem->setIcon(0, QIcon(":/images/open.png"));
		}
		pCurItem->setIcon(0, QIcon(":/images/Selected.png"));
		m_LastSelectItem = pCurItem;
		TreeItemData	ItemUserData;	
		ItemUserData = m_LastSelectItem->data(0, Qt::UserRole).value<TreeItemData>();
		m_CurSeltDevId = 0;
		
		if (TABLE_RDEVS_INFO == ItemUserData.ShowTableId)
		{
			m_CurSeltDevId = ItemUserData.RecordId;
		}
	
		ShowIndexInfo();
	}
}

//显示选中设备的点内容
void TransDlg::ShowIndexInfo()
{
	while (ui->IndexInfoTabWdgt->rowCount()>0)
	{
		ui->IndexInfoTabWdgt->removeRow(0);
	}

	if (m_CurSeltDevId<=0)
	{
		return ;
	}
	if (m_CurIoTableId != TABLE_RDEVS_YX &&
		m_CurIoTableId != TABLE_RDEVS_YC &&
		m_CurIoTableId != TABLE_RDEVS_YK &&
		m_CurIoTableId != TABLE_RDEVS_YM )
	{
		return ;
	}

	char			*buff= NULL;
	int			buff_len = 0;
	int			Ret = 0;
	QString			ItemText;
	Trans_ItemData	ItemData;
	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, 
																m_CurIoTableId, 
																m_CurSeltDevId+PARENT_OFFSET*TABLE_RDEVS_INFO,
																buff, 
																&buff_len, 
																true);
	if (Ret>0 && buff && buff_len>0)
	{
		for (int i=0; i<Ret; i++)
		{
			ItemData.DeviceId = m_CurSeltDevId;
			switch(m_CurIoTableId)
			{
			case TABLE_RDEVS_YX:
				{
					ItemData.IndexId = ((rDevS_Yx*)buff)[i].ID;
					ItemData.IndexNo = ((rDevS_Yx*)buff)[i].IndexNo;
					ItemText = QString::fromLocal8Bit(((rDevS_Yx*)buff)[i].Name);
				}
				break;
			case TABLE_RDEVS_YC:
				{
					ItemData.IndexId = ((rDevS_Yc*)buff)[i].ID;
					ItemData.IndexNo = ((rDevS_Yc*)buff)[i].IndexNo;
					ItemText = QString::fromLocal8Bit(((rDevS_Yc*)buff)[i].Name);
				}
				break;
			case TABLE_RDEVS_YK:
				{
					ItemData.IndexId = ((rDevS_Yk*)buff)[i].ID;
					ItemData.IndexNo = ((rDevS_Yk*)buff)[i].IndexNo;
					ItemText = QString::fromLocal8Bit(((rDevS_Yk*)buff)[i].Name);
				}
				break;
			case TABLE_RDEVS_YM:
				{
					ItemData.IndexId = ((rDevS_Ym*)buff)[i].ID;
					ItemData.IndexNo = ((rDevS_Ym*)buff)[i].IndexNo;
					ItemText = QString::fromLocal8Bit(((rDevS_Ym*)buff)[i].Name);
				}
				break;
			default:
				break;
			}
			
			if (!CheckIndexIsInTransInfo(ItemData.DeviceId, ItemData.IndexId))
			{
				AddOneIndexToShow(ItemData, ItemText);
			}
		}
		
	}
	if (buff)
	{
		free(buff);
		buff = NULL;
	}
}

//添加一条到显示
void TransDlg::AddOneIndexToShow(Trans_ItemData ItemData, QString IndexName)
{
	int rowCount = ui->IndexInfoTabWdgt->rowCount();
	ui->IndexInfoTabWdgt->insertRow(rowCount);
	QTableWidgetItem *pItem = NULL;
	QVariant		 ItemVarData;
	ItemVarData.setValue<Trans_ItemData>(ItemData);

	pItem = new QTableWidgetItem;
	
	pItem->setText(QString::fromLocal8Bit("%1").arg(ItemData.IndexNo));
	pItem->setData(Qt::UserRole, ItemVarData);
	ui->IndexInfoTabWdgt->setItem(rowCount, 0, pItem);

	pItem = new QTableWidgetItem;
	pItem->setText(IndexName);
	ui->IndexInfoTabWdgt->setItem(rowCount,	1, pItem);
}

//删除点信息
void TransDlg::DeleteOneIndexInfo(unsigned int DevId, unsigned int IndexId)
{
	int rowCount = ui->IndexInfoTabWdgt->rowCount();
	QTableWidgetItem *pItem = NULL;
	Trans_ItemData newData;
	
	for (int i=0; i<rowCount; i++)
	{
		pItem = NULL;
		pItem = ui->IndexInfoTabWdgt->item(i, 0);
		if (pItem)
		{
			newData = pItem->data(Qt::UserRole).value<Trans_ItemData>();
			if (newData.DeviceId == DevId &&
				newData.IndexId == IndexId)
			{
				ui->IndexInfoTabWdgt->removeRow(i);
				break;
			}
		}
	}
}

//显示转发信息
void TransDlg::ShowTransInfo()
{
	while (ui->TransInfoTabWdgt->rowCount()>0)
	{
		ui->TransInfoTabWdgt->removeRow(0);
	}

	if (m_CurTransTabId != TABLE_RTRANSMIT_YX &&
		m_CurTransTabId != TABLE_RTRANSMIT_YC &&
		m_CurTransTabId != TABLE_RTRANSMIT_YK &&
		m_CurTransTabId != TABLE_RTRANSMIT_YM )
	{
		return ;
	}
	if (m_TransInfoId<=0)
	{
		return ;
	}

	char			*buff= NULL;
	int			buff_len = 0;
	int			Ret = 0;
	QString			ItemText;
	Trans_ItemData	ItemData;
	int			RowCount = 0;
	Ret = g_DbCfgFunObj.GetRecordBuffbyAppType_TableId_ParentId(SYSTEM_TYPE_NSA_YD, 
																m_CurTransTabId,
																m_ParentId,
																buff, 
																&buff_len, 
																true);
	if (Ret>0 && buff && buff_len>0)
	{
		unsigned int		DevId = 0;
		unsigned int		IndexId = 0;
		unsigned short		IndexNo = 0;
		for (int i=0; i<Ret; i++)
		{
			ItemData.DataProp = TRANS_PROP_NONE;
			
			switch(m_CurTransTabId)
			{
			case TABLE_RTRANSMIT_YX:
				{
					ItemData.RecordId = ((rTransmit_Yx*)buff)[i].ID;
					ItemData.DeviceId = ((rTransmit_Yx*)buff)[i].m_devID%PARENT_OFFSET;
					ItemData.IndexId = ((rTransmit_Yx*)buff)[i].YxId%PARENT_OFFSET;
//					ItemData.TransNo = ((rTransmit_Yx*)buff)[i].YxNoScada;
					ItemData.TaskId = ((rTransmit_Yx*)buff)[i].m_taskID;
					ItemData.IndexNo = ((rTransmit_Yx*)buff)[i].IndexNo;
				}
				break;
			case TABLE_RTRANSMIT_YC:
				{
					ItemData.RecordId = ((rTransmit_Yc*)buff)[i].ID;
					ItemData.DeviceId = ((rTransmit_Yc*)buff)[i].m_devID%PARENT_OFFSET;
					ItemData.IndexId = ((rTransmit_Yc*)buff)[i].YcId%PARENT_OFFSET;
//					ItemData.TransNo = ((rTransmit_Yc*)buff)[i].YcNoScada;
					ItemData.TaskId = ((rTransmit_Yc*)buff)[i].m_taskID;
					ItemData.IndexNo = ((rTransmit_Yc*)buff)[i].IndexNo;
				}
				break;
			case TABLE_RTRANSMIT_YK:
				{
					ItemData.RecordId = ((rTransmit_Yk*)buff)[i].ID;
					ItemData.DeviceId = ((rTransmit_Yk*)buff)[i].m_devID%PARENT_OFFSET;
					ItemData.IndexId = ((rTransmit_Yk*)buff)[i].YkId%PARENT_OFFSET;
//					ItemData.TransNo = ((rTransmit_Yk*)buff)[i].YkNoScada;
					ItemData.TaskId = ((rTransmit_Yk*)buff)[i].m_taskID;
					ItemData.IndexNo = ((rTransmit_Yk*)buff)[i].IndexNo;
				}
				break;
			case TABLE_RTRANSMIT_YM:
				{
					ItemData.RecordId = ((rTransmit_Ym*)buff)[i].ID;
					ItemData.DeviceId = ((rTransmit_Ym*)buff)[i].m_devID%PARENT_OFFSET;
					ItemData.IndexId = ((rTransmit_Ym*)buff)[i].YmId%PARENT_OFFSET;
//					ItemData.TransNo = ((rTransmit_Ym*)buff)[i].YmNoScada;
					ItemData.TaskId = ((rTransmit_Ym*)buff)[i].m_taskID;
					ItemData.IndexNo = ((rTransmit_Ym*)buff)[i].IndexNo;
				}
				break;
			default:
				break;
			}
			ItemData.OldeTransNo = ItemData.TransNo;
			AddOneToTransInfoShow(ItemData, -1);
		}
	}

	if (buff)
	{
		free(buff);
		buff = NULL;
	}
}

//添加到转发信息显示
int TransDlg::AddOneToTransInfoShow(Trans_ItemData ItemData, int RowIndex)
{
	if (ItemData.DeviceId<=0 || ItemData.IndexId<=0 || m_CurIoTableId<=0)
	{
		return -1;
	}
	
	if (-1 == RowIndex)
	{
		RowIndex = ui->TransInfoTabWdgt->rowCount();
	}

	QString DevNamestr;
	QString IndexNameStr;
	char   *buff = NULL;
	int	buff_len = 0;
	int	Ret = 0;
	Ret = g_DbCfgFunObj.GetRecordNamebyAppType_TableId_RecordId(SYSTEM_TYPE_NSA_YD, 
																TABLE_RDEVS_INFO,
																ItemData.DeviceId,
																DevNamestr);
	Ret = g_DbCfgFunObj.GetRecordNamebyAppType_TableId_RecordId(SYSTEM_TYPE_NSA_YD, 
																m_CurIoTableId,
																ItemData.IndexId,
																IndexNameStr);
	ui->TransInfoTabWdgt->insertRow(RowIndex);
	QTableWidgetItem *pItem = NULL;
	QVariant		 ItemVarData;
	ItemVarData.setValue<Trans_ItemData>(ItemData);

	pItem = new QTableWidgetItem;
	pItem->setText(QString::fromLocal8Bit("%1").arg(ItemData.TransNo));
	pItem->setData(Qt::UserRole, ItemVarData);
	if (ItemData.DataProp != TRANS_PROP_NONE)
	{
		pItem->setData(Qt::TextColorRole, Qt::red);
	}
	ui->TransInfoTabWdgt->setItem(RowIndex, 0, pItem);
	ui->TransInfoTabWdgt->scrollToItem(pItem);

	pItem = new QTableWidgetItem;
	pItem->setText(DevNamestr);
	if (ItemData.DataProp != TRANS_PROP_NONE)
	{
		pItem->setData(Qt::TextColorRole, Qt::red);
	}
	ui->TransInfoTabWdgt->setItem(RowIndex,	1, pItem);

	pItem = new QTableWidgetItem;
	pItem->setText(IndexNameStr);
	if (ItemData.DataProp != TRANS_PROP_NONE)
	{
		pItem->setData(Qt::TextColorRole, Qt::red);
	}
	ui->TransInfoTabWdgt->setItem(RowIndex,	2, pItem);

	

	return 1;
}

//从转发信息显示中删除
int TransDlg::SetDeleteFlagTransInfo(int RowIndex, bool bDeleteFlag)
{
	if (RowIndex<0 || RowIndex >= ui->TransInfoTabWdgt->rowCount())
	{
		return 0;
	}
	QTableWidgetItem  *pTempItem = NULL;
	Trans_ItemData    ItemData;
	QVariant		  ItemVarData;
	QFont OldFont;
	pTempItem = ui->TransInfoTabWdgt->item(RowIndex, 0);
	if (pTempItem)
	{
		ItemData = pTempItem->data(Qt::UserRole).value<Trans_ItemData>();
		if (ItemData.bDeleteFlag != bDeleteFlag)
		{

			ItemData.bDeleteFlag = bDeleteFlag;
			ItemVarData.setValue<Trans_ItemData>(ItemData);
			pTempItem->setData(Qt::UserRole, ItemVarData);

			OldFont = pTempItem->font();
			if (bDeleteFlag)
			{
				OldFont.setItalic(true);
				pTempItem->setData(Qt::TextColorRole, Qt::magenta);
			}else
			{
				OldFont.setItalic(false);
				if (TRANS_PROP_NONE != ItemData.DataProp)
				{
					pTempItem->setData(Qt::TextColorRole, Qt::red);
				}else
				{
					pTempItem->setData(Qt::TextColorRole, Qt::black);
				}
			}

			pTempItem->setFont(OldFont);
			if (!m_bDeltTransFlag && bDeleteFlag)
			{
				m_bDeltTransFlag = true;
			}

		}
	}
	
	return 0;
}

//	获取选中点记录
int TransDlg::GetSelectIndexRow()
{
	QTableWidgetItem * pVerticalItem = NULL;
	int nRowCout = ui->IndexInfoTabWdgt->rowCount();

	QItemSelectionModel *pItemSelectModel = ui->IndexInfoTabWdgt->selectionModel();
	if (pItemSelectModel)
	{
		for (int i=0; i<nRowCout; i++)
		{
			if (pItemSelectModel->isRowSelected(i, ui->IndexInfoTabWdgt->rootIndex()))
			{
				return i;
			}
		}
	}
	return -1;
}

//	获取选中转发记录
int TransDlg::GetSelectTransRow()
{
	QTableWidgetItem * pVerticalItem = NULL;
	int nRowCout = ui->TransInfoTabWdgt->rowCount();

	QItemSelectionModel *pItemSelectModel = ui->TransInfoTabWdgt->selectionModel();
	if (pItemSelectModel)
	{
		for (int i=0; i<nRowCout; i++)
		{
			if (pItemSelectModel->isRowSelected(i, ui->TransInfoTabWdgt->rootIndex()))
			{
				return i;
			}
		}
	}
	return -1;
}

//调整转发序号
void TransDlg::AdjustTransNo()
{
	int nRowCout  = ui->TransInfoTabWdgt->rowCount();
	QTableWidgetItem *pItem = NULL;
	Trans_ItemData   ItemData;
	QVariant		 ItemVarData;
	for (int i=0; i<nRowCout; i++)
	{
		pItem = ui->TransInfoTabWdgt->item(i, 0);
		if (pItem)
		{	
			ItemData = pItem->data(Qt::UserRole).value<Trans_ItemData>();
			if (ItemData.TransNo != i)
			{
				ItemData.TransNo = i;
				if (ItemData.OldeTransNo != ItemData.TransNo)
				{
					if (TRANS_PROP_NONE == ItemData.DataProp)
					{
						ItemData.DataProp = TRANS_PROP_UPDATE;
						
						pItem->setData(Qt::TextColorRole, Qt::red);
					}
				}else
				{
					if (TRANS_PROP_UPDATE == ItemData.DataProp)
					{
						ItemData.DataProp = TRANS_PROP_NONE;
						pItem->setData(Qt::TextColorRole, Qt::black);
					}
				}
				ItemVarData.setValue<Trans_ItemData>(ItemData);
				pItem->setData(Qt::UserRole, ItemVarData);
				pItem->setText(QString::fromLocal8Bit("%1").arg(ItemData.TransNo));
			}
		}
	}
}

//检查当前点是否已存在转发表中
bool TransDlg::CheckIndexIsInTransInfo(unsigned int DevId, unsigned int IndexId)
{
	if (DevId<=0 || IndexId<=0)
	{
		return false;
	}
	int nRowCout  = ui->TransInfoTabWdgt->rowCount();
	QTableWidgetItem *pItem = NULL;
	Trans_ItemData   ItemData;
	for (int i=0; i<nRowCout; i++)
	{
		pItem = ui->TransInfoTabWdgt->item(i, 0);
		if (pItem)
		{	
			ItemData = pItem->data(Qt::UserRole).value<Trans_ItemData>();
			if (ItemData.DeviceId == DevId && ItemData.IndexId == IndexId)
			{
				return true;
				break;
			}
		}
	}
	
	return false;
}

//	获取选中点记录信息
void TransDlg::GetSelectIndexInfo()
{
	int SeltRowNo = 0;
	SeltRowNo = GetSelectIndexRow();
	if (SeltRowNo>=0 && ui->IndexInfoTabWdgt->rowCount()>0)
	{
		QTableWidgetItem *pSeltItem=NULL;
		Trans_ItemData	  itemdata;
		pSeltItem = ui->IndexInfoTabWdgt->item(SeltRowNo, 0);
		if (pSeltItem)
		{
			itemdata = pSeltItem->data(Qt::UserRole).value<Trans_ItemData>();
			m_CurSeltIoId = itemdata.IndexId;
		}
	}
}

//添加一条转发记录
int TransDlg::InsertOneTransinfoShow(unsigned int DevId, unsigned int IoId)
{
	if (DevId<=0 || IoId<=0)
	{
		return 0;
	}
	Trans_ItemData Newitemdata;
	int		   RowIndexNo = 0;
	bool		   bNeedAdjust = true;
	Newitemdata.DeviceId = DevId;
	Newitemdata.IndexId = IoId;
	
	int SeltTransRowNo = GetSelectTransRow();
	if (SeltTransRowNo<0)
	{
		SeltTransRowNo = ui->TransInfoTabWdgt->rowCount();
		bNeedAdjust = false;
	}
	Newitemdata.TransNo = SeltTransRowNo;
	Newitemdata.OldeTransNo = SeltTransRowNo;
	Newitemdata.DataProp = TRANS_PROP_INSERT;
	if (AddOneToTransInfoShow(Newitemdata, SeltTransRowNo)>0 )
	{
		DeleteOneIndexInfo(DevId, IoId);
	}
	if (bNeedAdjust)
	{
		AdjustTransNo();
	}
	return 1;
}

// void TransDlg::IndexTabClick()
// {
// 	m_CurSeltIoId = 0;
// 	GetSelectIndexInfo();
// }

void TransDlg::IndexTabDoubleClick()
{
	m_CurSeltIoId = 0;
	GetSelectIndexInfo();
	if (m_CurSeltDevId>0 && m_CurSeltIoId>0)
	{
		if (m_bDeltTransFlag)
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("请先保存修改内容！"), QMessageBox::Ok | QMessageBox::Cancel);
			return ;
		}
	
		InsertOneTransinfoShow(m_CurSeltDevId, m_CurSeltIoId);
		m_CurSeltIoId = 0;
	}
}

void TransDlg::AddSeltIoInfo()
{
	if (m_bDeltTransFlag)
	{
		QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("请先保存修改内容！"), QMessageBox::Ok | QMessageBox::Cancel);
		return ;
	}
	QTableWidgetItem *pSeltItem=NULL;
	Trans_ItemData	  itemdata;
	int nRowCout = ui->IndexInfoTabWdgt->rowCount();

	QItemSelectionModel *pItemSelectModel = ui->IndexInfoTabWdgt->selectionModel();
	if (pItemSelectModel)
	{
		for (int i=0; i<ui->IndexInfoTabWdgt->rowCount(); i++)
		{
			if (pItemSelectModel->isRowSelected(i, ui->IndexInfoTabWdgt->rootIndex()))
			{
				pSeltItem = ui->IndexInfoTabWdgt->item(i, 0);
				if (pSeltItem)
				{
					itemdata = pSeltItem->data(Qt::UserRole).value<Trans_ItemData>();
					m_CurSeltIoId = itemdata.IndexId;
					if (m_CurSeltDevId>0 && m_CurSeltIoId>0)
					{
						if (InsertOneTransinfoShow(m_CurSeltDevId, m_CurSeltIoId)>0)
						{
							i--;
						}
						m_CurSeltIoId = 0;
					}
				}
			}
		}
	}	
}

void TransDlg::AddAll()
{
	m_CurSeltIoId = 0;
	int rowcount = ui->IndexInfoTabWdgt->rowCount();
	if (m_CurSeltDevId>0 && rowcount>0)
	{
		if (m_bDeltTransFlag)
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("请先保存修改内容！"), QMessageBox::Ok | QMessageBox::Cancel);
			return ;
		}
		QTableWidgetItem *ptempItem = NULL;
		Trans_ItemData	  tempitemdata;
		while (ui->IndexInfoTabWdgt->rowCount()>0)
		{
			ptempItem = NULL;
			ptempItem = ui->IndexInfoTabWdgt->item(0,0);
			if (ptempItem)
			{
				tempitemdata = ptempItem->data(Qt::UserRole).value<Trans_ItemData>();
				if (tempitemdata.DeviceId>0 && tempitemdata.IndexId>0)
				{
					InsertOneTransinfoShow(tempitemdata.DeviceId, tempitemdata.IndexId);
				}
			}
		}
	}
}

void TransDlg::DeleteSeltTrans()
{
	int  oldRowCount = ui->TransInfoTabWdgt->rowCount();
	QItemSelectionModel *pItemSelectModel = ui->TransInfoTabWdgt->selectionModel();
	if (pItemSelectModel)
	{
		for (int i=0; i<ui->TransInfoTabWdgt->rowCount(); i++)
		{
			if (pItemSelectModel->isRowSelected(i, ui->TransInfoTabWdgt->rootIndex()))
			{
				SetDeleteFlagTransInfo(i, true);
			}
		}
	}
}

void TransDlg::DeleteAllTrans()
{
	for (int i=0; i<ui->TransInfoTabWdgt->rowCount(); i++)
	{
		SetDeleteFlagTransInfo(i, true);
	}
	if (ui->TransInfoTabWdgt->rowCount()>0)
	{
		if (!m_bDeltAllFlag)
		{
			m_bDeltAllFlag = true;
		}
	}
}

void TransDlg::CancelDeleteTrans()
{
	for (int i=0; i<ui->TransInfoTabWdgt->rowCount(); i++)
	{
		SetDeleteFlagTransInfo(i, false);
	}

	if (m_bDeltAllFlag)
	{
		m_bDeltAllFlag = false;
	}
	if (m_bDeltTransFlag)
	{
		m_bDeltTransFlag = false;
	}
	
}

//从数据库中删除一条转发记录
int TransDlg::DeleteOneFromTransDB(unsigned int RecordId)
{
	return  g_DbCfgFunObj.DeleteRecordByID(SYSTEM_TYPE_NSA_YD, m_CurTransTabId, RecordId);
}

////从数据库中删除全部转发记录
int TransDlg::DeleteAllFromTransDB()
{
	char *tableName = NULL;
	int   Name_len = 0;
	char sql[SQL_MAX_LEN];			
	memset(sql, 0, SQL_MAX_LEN);
	g_DbCfgFunObj.GetTableEngNamebyAppType_TableId(SYSTEM_TYPE_NSA_YD, m_CurTransTabId, tableName, &Name_len);
	if (tableName)
	{
		sprintf(sql, "delete from %s where ParentId=%u", tableName, m_ParentId);
		delete [](char*)tableName;
		tableName = NULL;
	}else
	{
		QMessageBox::information(NULL, QString::fromLocal8Bit("Warn"), QString::fromLocal8Bit("获取表名失败，表ID[%1], 应用类型[%1]").arg(m_CurTransTabId).arg(SYSTEM_TYPE_NSA_YD));
		return -2;
	}
	return g_pSqLiteDB.DB_Modify(sql);
	return 1;
}

//修改转发记录转发序号
int TransDlg::upDateTransNoDB(unsigned int RecordId, unsigned short TransNo)
{
	if (RecordId<=0 || TransNo<0)
	{
		return -1;
	}

	char sql[SQL_MAX_LEN];
	memset(sql, 0, SQL_MAX_LEN);

	switch (m_CurIoTableId)
	{
	case TABLE_RDEVS_YX:
		{
			sprintf(sql, "update %s set YxNoScada=%d  where ID=%d", m_CurTransTableName, TransNo, RecordId);
		}
		break;
	case TABLE_RDEVS_YC:
		{
			sprintf(sql, "update %s set YcNoScada=%d  where ID=%d", m_CurTransTableName, TransNo, RecordId);
		}
		break;
	case TABLE_RDEVS_YK:
		{
			sprintf(sql, "update %s set YkNoScada=%d  where ID=%d", m_CurTransTableName, TransNo, RecordId);
		}
		break;
	case TABLE_RDEVS_YM:
		{
			sprintf(sql, "update %s set YmNoScada=%d  where ID=%d", m_CurTransTableName, TransNo, RecordId);
		}
		break;
	default:
		break;
	}
	return g_pSqLiteDB.DB_Modify(sql);

	return 0;
}


//插入转发记录
int TransDlg::insertTransInfoDB(unsigned int DevId, unsigned int IndexId, unsigned short TransNo)
{
	if (!m_ColInfo.IsSafe() || !m_RecordBuff || DevId<=0 || IndexId<=0 || TransNo<0 || m_CurTransTabId<=0)
	{
		return -1;
	}
	unsigned int RecordId=0;
	g_DbCfgFunObj.SetDefaultData(m_RecordBuff, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, m_TransInfoId+PARENT_OFFSET*TABLE_RTASK_INFO);
	g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, m_CurTransTabId, RecordId);
	switch (m_CurTransTabId)
	{
	case TABLE_RTRANSMIT_YX:
		{
			((rTransmit_Yx*)m_RecordBuff)->ID = RecordId;
			((rTransmit_Yx*)m_RecordBuff)->ParentId = m_ParentId/*+PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO*/;
			((rTransmit_Yx*)m_RecordBuff)->m_devID = DevId+PARENT_OFFSET*TABLE_RDEVS_INFO;
			((rTransmit_Yx*)m_RecordBuff)->YxId = IndexId+PARENT_OFFSET*TABLE_RDEVS_YX;
//			((rTransmit_Yx*)m_RecordBuff)->YxNoScada = TransNo;

			g_DbCfgFunObj.GetTaskIdAndDevIdAndIndexNoByIndexId(TABLE_RDEVS_YX,((rTransmit_Yx*)m_RecordBuff)->YxId,((rTransmit_Yx*)m_RecordBuff)->m_taskID,((rTransmit_Yx*)m_RecordBuff)->m_devID,((rTransmit_Yx*)m_RecordBuff)->IndexNo);
		}
		break;
	case TABLE_RTRANSMIT_YC:
		{
			((rTransmit_Yc*)m_RecordBuff)->ID = RecordId;
			((rTransmit_Yc*)m_RecordBuff)->ParentId = m_ParentId/*+PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO*/;
			((rTransmit_Yc*)m_RecordBuff)->m_devID = DevId+PARENT_OFFSET*TABLE_RDEVS_INFO;
			((rTransmit_Yc*)m_RecordBuff)->YcId = IndexId+PARENT_OFFSET*TABLE_RDEVS_YC;
//			((rTransmit_Yc*)m_RecordBuff)->YcNoScada = TransNo;

			g_DbCfgFunObj.GetTaskIdAndDevIdAndIndexNoByIndexId(TABLE_RDEVS_YC,((rTransmit_Yc*)m_RecordBuff)->YcId,((rTransmit_Yc*)m_RecordBuff)->m_taskID,((rTransmit_Yc*)m_RecordBuff)->m_devID,((rTransmit_Yc*)m_RecordBuff)->IndexNo);
		}
		break;
	case TABLE_RTRANSMIT_YK:
		{
			((rTransmit_Yk*)m_RecordBuff)->ID = RecordId;
			((rTransmit_Yk*)m_RecordBuff)->ParentId = m_ParentId/*+PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO*/;
			((rTransmit_Yk*)m_RecordBuff)->m_devID = DevId+PARENT_OFFSET*TABLE_RDEVS_INFO;
			((rTransmit_Yk*)m_RecordBuff)->YkId = IndexId+PARENT_OFFSET*TABLE_RDEVS_YK;
//			((rTransmit_Yk*)m_RecordBuff)->YkNoScada = TransNo;

			g_DbCfgFunObj.GetTaskIdAndDevIdAndIndexNoByIndexId(TABLE_RDEVS_YK,((rTransmit_Yk*)m_RecordBuff)->YkId,((rTransmit_Yk*)m_RecordBuff)->m_taskID,((rTransmit_Yx*)m_RecordBuff)->m_devID,((rTransmit_Yk*)m_RecordBuff)->IndexNo);
		}
		break;
	case TABLE_RTRANSMIT_YM:
		{
			((rTransmit_Ym*)m_RecordBuff)->ID = RecordId;
			((rTransmit_Ym*)m_RecordBuff)->ParentId = m_ParentId/*+PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO*/;
			((rTransmit_Ym*)m_RecordBuff)->m_devID = DevId+PARENT_OFFSET*TABLE_RDEVS_INFO;
			((rTransmit_Ym*)m_RecordBuff)->YmId = IndexId+PARENT_OFFSET*TABLE_RDEVS_YM;
//			((rTransmit_Ym*)m_RecordBuff)->YmNoScada = TransNo;

			g_DbCfgFunObj.GetTaskIdAndDevIdAndIndexNoByIndexId(TABLE_RDEVS_YM,((rTransmit_Ym*)m_RecordBuff)->YmId,((rTransmit_Ym*)m_RecordBuff)->m_taskID,((rTransmit_Ym*)m_RecordBuff)->m_devID,((rTransmit_Ym*)m_RecordBuff)->IndexNo);
		}
		break;
	}
	if (RecordId<=0)
	{
		return -3;
	}

	return g_DbCfgFunObj.InsertRecordIntoDB(SYSTEM_TYPE_NSA_YD, m_CurTransTabId, m_RecordBuff, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, m_CurTransTableName);

}

void TransDlg::SaveBtnClick()
{
	if (m_bDeltAllFlag)
	{
		//DeleteAllFromTransDB();
		DeleteAllTrans();
	//	m_bDeltAllFlag = false;	
	}
	SaveData();
	//SaveData_New();
	ShowTransInfo();
	ShowIndexInfo();
}

void TransDlg::Affirm()
{
	SaveData();
	//SaveData_New();
	close();
}
void TransDlg::SaveData()
{
	int rowcount = ui->TransInfoTabWdgt->rowCount();
	if (rowcount<=0)
	{
		return ;
	}

	if (m_RecordBuff)
	{
		delete [](char*)m_RecordBuff;
		m_RecordBuff = NULL;
	}
	QTableWidgetItem *pItem = NULL;
	Trans_ItemData   itemdata;
	if (m_bDeltTransFlag)
	{
		g_pSqLiteDB.BeginTransaction();
		for (int i=0; i<rowcount; i++)
		{
			pItem = NULL;
			pItem = ui->TransInfoTabWdgt->item(i, 0);
			if (!pItem)
			{
				break;
			}
			itemdata = pItem->data(Qt::UserRole).value<Trans_ItemData>();
			if (itemdata.bDeleteFlag)
			{
				if (itemdata.RecordId>0)
				{
					DeleteOneFromTransDB(itemdata.RecordId);
				}
				ui->TransInfoTabWdgt->removeRow(i);
				i--;
			}
		}
		AdjustTransNo();
		g_pSqLiteDB.CommitTransaction();
		m_bDeltTransFlag = false;
		rowcount = ui->TransInfoTabWdgt->rowCount();
	}
	if (rowcount<=0)
	{
		return ; 
	}
	memset(m_CurTransTableName, 0, TABLE_ENG_NAME_LEN);
	g_DbCfgFunObj.GetTableEngNamebyAppType_TableId(SYSTEM_TYPE_NSA_YD, m_CurTransTabId, m_CurTransTableName);
	if (QString::fromLocal8Bit(m_CurTransTableName).length()<=0)
	{
		return;
	}

	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, m_CurTransTabId);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return ;
	}
	if (m_ColInfo.m_RecordLength>0)
	{
		m_RecordBuff = new char[m_ColInfo.m_RecordLength];
		memset(m_RecordBuff, 0, m_ColInfo.m_RecordLength);
	}

	g_pSqLiteDB.BeginTransaction();
	for (int i=0; i<rowcount; i++)
	{
		pItem = ui->TransInfoTabWdgt->item(i, 0);
		itemdata = pItem->data(Qt::UserRole).value<Trans_ItemData>();
		if ( TRANS_PROP_UPDATE == itemdata.DataProp && itemdata.RecordId>0)
		{
			upDateTransNoDB(itemdata.RecordId,itemdata.TransNo);
		}else if ( TRANS_PROP_INSERT == itemdata.DataProp)
		{
			insertTransInfoDB(itemdata.DeviceId, itemdata.IndexId, itemdata.TransNo);
		}
	}
	g_pSqLiteDB.CommitTransaction();
	m_ColInfo.ReleasColumnInfo();
	if (m_RecordBuff)
	{
		delete [](char*)m_RecordBuff;
		m_RecordBuff = NULL;
	}
}

void TransDlg::SaveData_New()
{
	int rowcount = ui->TransInfoTabWdgt->rowCount();
	if (rowcount<=0)
	{
		return ;
	}

	if (m_RecordBuff)
	{
		delete [](char*)m_RecordBuff;
		m_RecordBuff = NULL;
	}

	memset(m_CurTransTableName, 0, TABLE_ENG_NAME_LEN);
	g_DbCfgFunObj.GetTableEngNamebyAppType_TableId(SYSTEM_TYPE_NSA_YD, m_CurTransTabId, m_CurTransTableName);
	if (QString::fromLocal8Bit(m_CurTransTableName).length()<=0)
	{
		return;
	}
	if (m_bDeltAllFlag)
	{
		DeleteAllFromTransDB();
		m_bDeltAllFlag = false;
		return ;
	}

	QTableWidgetItem *pItem = NULL;
	Trans_ItemData   itemdata;
	if (m_bDeltTransFlag)
	{
		for (int i=0; i<rowcount; i++)
		{
			pItem = NULL;
			pItem = ui->TransInfoTabWdgt->item(i, 0);
			if (!pItem)
			{
				break;
			}
			itemdata = pItem->data(Qt::UserRole).value<Trans_ItemData>();
			if (itemdata.bDeleteFlag)
			{
				if (itemdata.RecordId>0)
				{
					DeleteOneFromTransDB_New(itemdata.RecordId);
				}
				ui->TransInfoTabWdgt->removeRow(i);
				i--;
			}
		}
		AdjustTransNo();
		m_bDeltTransFlag = false;
		rowcount = ui->TransInfoTabWdgt->rowCount();
	}
	CommitDeleteOper();
	if (rowcount<=0)
	{
		return ; 
	}
	
	m_ColInfo.InitColumnInfo(SYSTEM_TYPE_NSA_YD, m_CurTransTabId);
	if (!m_ColInfo.IsSafe())
	{
		m_ColInfo.ReleasColumnInfo();
		return ;
	}
	if (m_ColInfo.m_RecordLength>0)
	{
		m_RecordBuff = new char[m_ColInfo.m_RecordLength];
		memset(m_RecordBuff, 0, m_ColInfo.m_RecordLength);
	}
	
	g_DbCfgFunObj.GetNewFreeId(SYSTEM_TYPE_NSA_YD, m_CurTransTabId, m_StartRecordId);
	for (int i=0; i<rowcount; i++)
	{
		pItem = ui->TransInfoTabWdgt->item(i, 0);
		itemdata = pItem->data(Qt::UserRole).value<Trans_ItemData>();
		if ( TRANS_PROP_UPDATE == itemdata.DataProp && itemdata.RecordId>0)
		{
			upDateTransNoDB_New(itemdata.RecordId, itemdata.TransNo);
		}else if ( TRANS_PROP_INSERT == itemdata.DataProp)
		{
			insertTransInfoDB_New(itemdata.DeviceId, itemdata.IndexId, itemdata.TransNo);
		}
	}
	CommitUpDateOper();
	CommitInserterOper();

	m_ColInfo.ReleasColumnInfo();
	if (m_RecordBuff)
	{
		delete [](char*)m_RecordBuff;
		m_RecordBuff = NULL;
	}
	QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("保存修改完成！"), QMessageBox::Ok | QMessageBox::Cancel);
}

//从数据库中删除一条转发记录
int TransDlg::DeleteOneFromTransDB_New(unsigned int RecordId)
{
	QString deltStr;
	if (m_deltSqlStr.length()>0)
	{
		m_deltSqlStr += QString::fromLocal8Bit(";");
	}

	deltStr = QString::fromLocal8Bit("delete from %1 where ID=%2")
		.arg(QString::fromLocal8Bit(m_CurTransTableName))
		.arg(RecordId);

	if ( ( deltStr.toLocal8Bit().length()+m_deltSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
	{
		CommitDeleteOper();
	}
	m_deltSqlStr += deltStr;

	return 0;
}

//修改转发记录转发序号
int TransDlg::upDateTransNoDB_New(unsigned int RecordId, unsigned short TransNo)
{
	/*
	if (RecordId<=0 || TransNo<0)
	{
		return -1;
	}

	QString Str;

	switch (m_CurIoTableId)
	{
	case TABLE_RTRANSMIT_YX:
		{
			Str = QString::fromLocal8Bit("update %1 set YxNoScada=%2 where ID=%3")
				  .arg(QString::fromLocal8Bit(m_CurTransTableName))
				  .arg(TransNo)
				  .arg(RecordId);
		}
		break;
	case TABLE_RTRANSMIT_YC:
		{
			Str = QString::fromLocal8Bit("update %1 set YcNoScada=%2 where ID=%3")
				  .arg(QString::fromLocal8Bit(m_CurTransTableName))
				  .arg(TransNo)
				  .arg(RecordId);
		}
		break;
	case TABLE_RTRANSMIT_YK:
		{
			Str = QString::fromLocal8Bit("update %1 set YkNoScada=%2 where ID=%3")
				  .arg(QString::fromLocal8Bit(m_CurTransTableName))
				  .arg(TransNo)
				  .arg(RecordId);
		}
		break;
	case TABLE_RTRANSMIT_YM:
		{
			Str = QString::fromLocal8Bit("update %1 set YmNoScada=%2 where ID=%3")
				  .arg(QString::fromLocal8Bit(m_CurTransTableName))
				  .arg(TransNo)
				  .arg(RecordId);
		}
		break;
	default:
		break;
	}

	if ( ( Str.toLocal8Bit().length()+m_updateSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
	{
		CommitUpDateOper();
	}

	if  (m_updateSqlStr.length()>0)
	{
		m_updateSqlStr += QString::fromLocal8Bit(";");
	}
	m_updateSqlStr += Str;
	*/
	return 0;
}

//插入转发记录
int TransDlg::insertTransInfoDB_New(unsigned int DevId, unsigned int IndexId, unsigned short TransNo)
{
	if (!m_ColInfo.IsSafe() || !m_RecordBuff || DevId<=0 || IndexId<=0 || TransNo<0 || m_CurTransTabId<=0)
	{
		return -1;
	}
//	unsigned int RecordId=0;
	g_DbCfgFunObj.SetDefaultData(m_RecordBuff, m_ColInfo.m_pColumnInfo, m_ColInfo.m_pDataOffset, m_ColInfo.m_columnNum, m_ColInfo.m_RecordLength, m_TransInfoId+PARENT_OFFSET*TABLE_RTASK_INFO);
	switch (m_CurTransTabId)
	{
	case TABLE_RTRANSMIT_YX:
		{
			((rTransmit_Yx*)m_RecordBuff)->ID = m_StartRecordId;
			((rTransmit_Yx*)m_RecordBuff)->ParentId = m_ParentId/*+PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO*/;
			((rTransmit_Yx*)m_RecordBuff)->m_devID = DevId+PARENT_OFFSET*TABLE_RDEVS_INFO;
			((rTransmit_Yx*)m_RecordBuff)->YxId = IndexId+PARENT_OFFSET*TABLE_RDEVS_YX;
			//((rTransmit_Yx*)m_RecordBuff)->YxNoScada = TransNo;

			g_DbCfgFunObj.GetTaskIdAndDevIdAndIndexNoByIndexId(TABLE_RDEVS_YX,((rTransmit_Yx*)m_RecordBuff)->YxId,((rTransmit_Yx*)m_RecordBuff)->m_taskID,((rTransmit_Yx*)m_RecordBuff)->m_devID,((rTransmit_Yx*)m_RecordBuff)->IndexNo);
		}
		break;
	case TABLE_RTRANSMIT_YC:
		{
			((rTransmit_Yc*)m_RecordBuff)->ID = m_StartRecordId;
			((rTransmit_Yc*)m_RecordBuff)->ParentId = m_ParentId/*+PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO*/;
			((rTransmit_Yc*)m_RecordBuff)->m_devID = DevId+PARENT_OFFSET*TABLE_RDEVS_INFO;
			((rTransmit_Yc*)m_RecordBuff)->YcId = IndexId+PARENT_OFFSET*TABLE_RDEVS_YC;
			//((rTransmit_Yc*)m_RecordBuff)->YcNoScada = TransNo;

			g_DbCfgFunObj.GetTaskIdAndDevIdAndIndexNoByIndexId(TABLE_RDEVS_YC,((rTransmit_Yc*)m_RecordBuff)->YcId,((rTransmit_Yc*)m_RecordBuff)->m_taskID,((rTransmit_Yc*)m_RecordBuff)->m_devID,((rTransmit_Yc*)m_RecordBuff)->IndexNo);
		}
		break;
	case TABLE_RTRANSMIT_YK:
		{
			((rTransmit_Yk*)m_RecordBuff)->ID = m_StartRecordId;
			((rTransmit_Yk*)m_RecordBuff)->ParentId = m_ParentId/*+PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO*/;
			((rTransmit_Yk*)m_RecordBuff)->m_devID = DevId+PARENT_OFFSET*TABLE_RDEVS_INFO;
			((rTransmit_Yk*)m_RecordBuff)->YkId = IndexId+PARENT_OFFSET*TABLE_RDEVS_YK;
			//((rTransmit_Yk*)m_RecordBuff)->YkNoScada = TransNo;

			g_DbCfgFunObj.GetTaskIdAndDevIdAndIndexNoByIndexId(TABLE_RDEVS_YK,((rTransmit_Yk*)m_RecordBuff)->YkId,((rTransmit_Yk*)m_RecordBuff)->m_taskID,((rTransmit_Yk*)m_RecordBuff)->m_devID,((rTransmit_Yk*)m_RecordBuff)->IndexNo);
		}
		break;
	case TABLE_RTRANSMIT_YM:
		{
			((rTransmit_Ym*)m_RecordBuff)->ID = m_StartRecordId;
			((rTransmit_Ym*)m_RecordBuff)->ParentId = m_ParentId/*+PARENT_OFFSET*TABLE_RTRANSMITCONFIG_INFO*/;
			((rTransmit_Ym*)m_RecordBuff)->m_devID = DevId+PARENT_OFFSET*TABLE_RDEVS_INFO;
			((rTransmit_Ym*)m_RecordBuff)->YmId = IndexId+PARENT_OFFSET*TABLE_RDEVS_YM;
			//((rTransmit_Ym*)m_RecordBuff)->YmNoScada = TransNo;

			g_DbCfgFunObj.GetTaskIdAndDevIdAndIndexNoByIndexId(TABLE_RDEVS_YM,((rTransmit_Ym*)m_RecordBuff)->YmId,((rTransmit_Ym*)m_RecordBuff)->m_taskID,((rTransmit_Ym*)m_RecordBuff)->m_devID,((rTransmit_Ym*)m_RecordBuff)->IndexNo);
		}
		break;
	}
	if (m_StartRecordId<=0)
	{
		return -3;
	}

	QString InsertValSqlStr;
	g_DbCfgFunObj.GetValuesSql((char*)m_RecordBuff, m_ColInfo.m_RecordLength, m_ColInfo.m_pColumnInfo,  m_ColInfo.m_pDataOffset,  m_ColInfo.m_columnNum, OPER_ROW_INSERT, InsertValSqlStr);
	if (InsertValSqlStr.length()<=0)
	{
	  return -3;
	}
	if ( ( m_insertSqlStr.toLocal8Bit().length()+InsertValSqlStr.toLocal8Bit().length() ) >= SQL_MAX_LEN*2 )
	{
		CommitInserterOper();
	}

	if (m_insertSqlStr.length()<=0)
	{
		QString InsertHeadSqlStr;
		g_DbCfgFunObj.GetInsertHeadSql(SYSTEM_TYPE_NSA_YD, m_CurTransTabId, m_CurTransTableName, InsertHeadSqlStr);
		m_insertSqlStr = InsertHeadSqlStr;
	}else
	{
		m_insertSqlStr += QString::fromLocal8Bit(",");
	}
	 m_insertSqlStr += InsertValSqlStr;
	 m_StartRecordId += 1;
	return 1;
}

void TransDlg::CommitDeleteOper()
{
	if (m_deltSqlStr.length()>0)
	{
		memset(m_sql, 0, SQL_MAX_LEN*2);
		sprintf(m_sql, "%s", m_deltSqlStr.toLocal8Bit().data());
		int Result = g_pSqLiteDB.DB_Modify(m_sql);
		if (Result<0)
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("删除转发记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
		}
	//	qDebug()<<Result<<QString::fromLocal8Bit(m_sql);
		m_deltSqlStr.clear();
	}
}

void TransDlg::CommitUpDateOper()
{
	if (m_updateSqlStr.length()>0)
	{
		memset(m_sql, 0, SQL_MAX_LEN*2);
		sprintf(m_sql, "%s", m_updateSqlStr.toLocal8Bit().data());
		int Result = g_pSqLiteDB.DB_Modify(m_sql);
		if (Result<0)
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("修改转发记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
		}
	//	qDebug()<<Result<<QString::fromLocal8Bit(m_sql);
		m_updateSqlStr.clear();
	}
}

void TransDlg::CommitInserterOper()
{
	if (m_insertSqlStr.length()>0)
	{
		memset(m_sql, 0, SQL_MAX_LEN*2);
		sprintf(m_sql, "%s", m_insertSqlStr.toLocal8Bit().data());
		int Result = g_pSqLiteDB.DB_Modify(m_sql);
		if (Result<0)
		{
			QMessageBox::information(this, QString::fromLocal8Bit("数据库组态"), QString::fromLocal8Bit("插入转发记录失败！"), QMessageBox::Ok | QMessageBox::Cancel);
		}
	//	qDebug()<<Result<<QString::fromLocal8Bit(m_sql);
		m_insertSqlStr.clear();
	}
}
